﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;

namespace NetCoreCMS.Web.Areas.WebApi.Controllers
{
    public class User_TransferController : UserController
    {
        public ITransferService _transfer { get; set; }
        [Authorize]
        [HttpGet]
        public IActionResult Get(string user_name,decimal point,int payment_id=0)
        {
            //检查用户是否登录
            var model = GetUserInfo();
            if (model == null)
            {
                return Ok(new { status = 0, msg = "用户尚未登录或已超时！" });
            }
            var ucfg = Common.XMLConfig.Userconfig.loadConfig(_env.ContentRootPath);
            if (ucfg.transfer == 0)
            {
                return Ok(new { status = 0, msg = "未开启转让功能！" });
            }
            var touser = _users.GetModel(user_name);
            if (touser == null)
            {
                return Ok(new { status = 0, msg = "转出的会员不存在！" });
            }
            if (touser.id == model.id)
            {
                return Ok(new { status = 0, msg = "不能转向自己！" });
            }
            //检查站点目录
            if (point <= 0)
            {
                return Ok(new { status = 0, msg = "请输入正确的积分！" });
            }
            //转分
            if (model.cash_point < point)//不足
            {
                return Ok(new { status = 0, msg = "金额不足！" });
            }
            #region Transfer
            Transfer tf = new Transfer();
            tf.from_user_id = model.id;
            tf.from_user_name = model.user_name;
            tf.to_user_id = touser.id;
            tf.to_user_name = touser.user_name;
            tf.point = point;
            tf.remark = "奖金转让";
            bool flag = _transfer.Add(tf);
            #endregion
            if (flag)
            {
                //保存成功后返回
                return Ok(new { status = 1, msg = "转让成功！" });
            }
            else
            {
                //保存成功后返回
                return Ok(new { status = 0, msg = "转让失败！" });
            }
        }
    }
}